﻿using Domain;
using Domain.Entity;
using Domain.Repository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys
{
    public class SSRepository : ISSRepository
    {
        private readonly MyDbContext myDbContext;

        public SSRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }

        public async Task<bool> AddSSAsync(SS ss)
        {
            myDbContext.SS.Add(ss);
            return await myDbContext.SaveChangesAsync() > 0;
        }

        public async Task<List<SS>> SelectSSAsync()
        {
            return await myDbContext.SS.ToListAsync();
        }

        public async Task<bool> UpdateSSAsync(SS ss)
        {
            SS sss = myDbContext.SS.Where(e => e.SsdId == ss.SsdId).FirstOrDefault();
            if (sss != null)
            {
                sss.Checker = ss.Checker;
                sss.CheckTime = ss.CheckTime;
                sss.CheckStatus = ss.CheckStatus;
                sss.ChangeStatus = ss.ChangeStatus;
                sss.CheckComment = ss.CheckComment;
                myDbContext.SS.Update(sss);
            }
            return await myDbContext.SaveChangesAsync() > 0;
        }

        public SSFenYE FenYeSSAsync(string CheckStatus, int page, int pageSize)
        {
            int totalRecords = myDbContext.SS.Where(u => u.CheckStatus == CheckStatus).Count(); // 获取数据总量
            var SS = myDbContext.SS.Where(u => u.CheckStatus == CheckStatus).Skip((page - 1) * pageSize).Take(pageSize).ToList();
            return new SSFenYE
            {
                totalRecords = totalRecords,
                ss = SS
            };
        }

        public SSFenYE DateSSAsync(string StandardId, string gzc, DateTime startDate, DateTime endDate, int page, int pageSize, string ChangeStatus)
        {
            var query = myDbContext.SS.AsQueryable();

            if (!string.IsNullOrEmpty(StandardId))
            {
                query = query.Where(s => s.StandardId == StandardId);
            }
            if (!string.IsNullOrEmpty(ChangeStatus))
            {
                query = query.Where(s => s.ChangeStatus == ChangeStatus);
            }
            if (!string.IsNullOrEmpty(gzc))
            {
                query = query.Where(s => s.StandardName.Contains(gzc));
            }

            if (startDate != default && endDate != default)
            {
                query = query.Where(s => s.RegistTime >= startDate && s.RegistTime <= endDate);
            }

            int totalRecords = query.Count();
            var SS = query.Skip((page - 1) * pageSize).Take(pageSize).ToList();
            return new SSFenYE
            {
                totalRecords = totalRecords,
                ss = SS
            };
        }

        public async Task<bool> UpdateSSDesignerAsync(SS ss)
        {
            SS sss = myDbContext.SS.Where(e => e.SsdId == ss.SsdId).FirstOrDefault();
            if (sss != null)
            {
                sss.StandardName = ss.StandardName;
                sss.Changer = ss.Changer;
                sss.CheckStatus = ss.CheckStatus;
                sss.Designer = ss.Designer;
                sss.ChangeTime = ss.ChangeTime;
                sss.SalarySum = ss.SalarySum;
                sss.CheckStatus = ss.CheckStatus;
                sss.ChangeStatus = ss.ChangeStatus;
                sss.Remark = ss.Remark;
                myDbContext.SS.Update(sss);
            }
            return await myDbContext.SaveChangesAsync() > 0;
        }


		public async Task<string> SelectById(int id)
		{
			return await myDbContext.SS.Where(e => e.SsdId == id).Select(ss => ss.StandardName).FirstOrDefaultAsync();
		}

		public async Task<SS> SelectSalarySum(string id)
		{
            return await myDbContext.SS.Where(e => e.StandardId == id).FirstOrDefaultAsync();
		}
	}
}
